package com.java.framework.utils.http;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeConverter;

import com.google.common.collect.Maps;
import com.java.framework.utils.page.Pager;
import com.java.framework.utils.reflect.Reflections;

public class RequestUtils {
	
	private static Logger log = LoggerFactory.getLogger(RequestUtils.class);
	
	/**
	 * 从request中取查询条件
	 * 		1、查询条件以sp_开头
	 * 		2、查询条件包含三部分
	 * 			a、sp_
	 * 			b、字段名，可以是a.b.x的形式也可以是单个字段名
	 * 			c、匹配方式，如：_EQ，请参Criterion.Operator中的取值范围
	 * @param request
	 * @return map
	 */
	public static Map<String, Object> getSearchParams(HttpServletRequest request, TypeConverter converter, Class<?> refClass) {
		Map<String, Object> searchParams = Maps.newHashMap();
		Enumeration<String> names = request.getParameterNames();
		String value = "";
		String name = "";
		while(names.hasMoreElements()){
			name = names.nextElement();
			if(name.startsWith("sp_")) {
				value = StringUtils.trimToNull(request.getParameter(name));//去掉空格
				//TODO:处理特殊字符等等操作
				if (StringUtils.isNotBlank(value)) {//如果不为空
					name = StringUtils.substringAfter(name, "sp_");
					// 拆分Operator与filedName
					String[] ns = StringUtils.split(name, "_");
					if (ns.length != 2) {
						throw new IllegalArgumentException(name + " is not a valid search filter name");
					}
					String filedName = ns[0];
					String operatorName = ns[1];
					Class<?> type = refClass;
					try{
						type=Reflections.getMultiFiledType(type,filedName);
					}catch(NoSuchFieldException | SecurityException e){
						log.error( e.getMessage() );
						continue;
					}
					Object val ;
					if("IN".equals( operatorName ) || "NIN".equals( operatorName ) ){
						val = value;
					}else{
						val = converter.convertIfNecessary(value, type);
					}
					searchParams.put(name, val);
				}
			}
		}
		return searchParams;
	}

	/***
	 * 获取页面请求的分页参数
	 * 
	 * @param request
	 * @return
	 */
	public static <T> Pager<T> getPager(HttpServletRequest request) {
		return getPager(request, "currentPage", "pageSize");
	}

	/***
	 * 获取页面请求的分页参�?
	 * 
	 * @param request
	 * @param currentPageName
	 * @param pageSizeName
	 * @return
	 */
	public static <T> Pager<T> getPager(HttpServletRequest request,
			String currentPageName, String pageSizeName) {
		int pageSize = Pager.DEFAULT_PAGE_SIZE; //每页显示10
		int currentPage = Pager.DEFAULT_CURRENT_PAGE; //页码
		// 页面显示的行
		String pageSizeStr = StringUtils.trimToNull( request.getParameter(pageSizeName) );
		if (StringUtils.isNumeric(pageSizeStr)) {
			pageSize=Integer.parseInt(pageSizeStr);
		}

		// 当前页
		String currentPageStr =StringUtils.trimToNull(  request.getParameter(currentPageName) );
		if (StringUtils.isNumeric(currentPageStr)) {
			currentPage=Integer.parseInt(currentPageStr);
		}

		return new Pager<T>(currentPage, pageSize);
	}

}
